import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

def gamma_correction(image, gamma=2.2):
    """对图像进行伽马校正"""
    # 归一化到 [0, 1] 范围
    image_normalized = image / 255.0
    # 应用伽马校正
    corrected = np.power(image_normalized, 1.0 / gamma)
    # 还原到 [0, 255] 范围
    return (corrected * 255).astype(np.uint8)

# 读取图片（转为RGB格式）
image = np.array(Image.open("face.png").convert("RGB"))

# 应用伽马2.2校正
gamma_corrected = gamma_correction(image, gamma=2.2)

# 显示结果
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.title("原图")
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(gamma_corrected)
plt.title("伽马2.2校正后")
plt.axis('off')
plt.show()

# 保存结果
Image.fromarray(gamma_corrected).save("gamma_corrected.png")